﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace Daneas.Utility.Universals
{
    /// <summary>
    /// 生成静态html
    /// </summary>
    public class ToHtml
    {
        ///<summary>
        /// 生成HTML页（模板页上面的数组是$Porschev[i]$）
        ///</summary>
        ///<param name="temPath">模版页路径</param>
        ///<param name="arr">替换数组</param>
        ///<param name="createPath">生成HTML路径</param>
        public static bool CreateHtml(string temPath, string[] arr, string createPath)
        {
            string fileName = string.Empty;         //生成文件名
            string absoCrePath = string.Empty;      //生成页绝对路径
            string absoTemPath = string.Empty;      //模版页的绝对中径
            int s = arr.Count<string>();

            try
            {
                absoCrePath = System.Web.HttpContext.Current.Server.MapPath(createPath);

                if (!Directory.Exists(absoCrePath))
                {
                    Directory.CreateDirectory(absoCrePath);
                }
                absoTemPath = System.Web.HttpContext.Current.Server.MapPath(temPath);

                FileStream fs = File.Open(absoTemPath, FileMode.Open, FileAccess.Read);  //读取模版页
                StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
                StringBuilder sb = new StringBuilder(sr.ReadToEnd());
                sr.Close();

                for (int i = 0; i < s; i++)
                {
                    sb.Replace("$Porschev[" + i + "]$", arr[i]);
                }
                string absoPath;

                absoPath = System.Web.HttpContext.Current.Server.MapPath(createPath);
                if (!Directory.Exists(absoPath))
                {
                    Directory.CreateDirectory(absoPath);
                }

                fileName = DateTime.Now.ToFileTime().ToString() + ".html";          //设置文件名（这里可以根据需要变化命名）
                FileStream cfs = File.Create(absoCrePath + "/" + fileName);
                StreamWriter sw = new StreamWriter(cfs, Encoding.GetEncoding("utf-8"));
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();

                return true;
            }
            catch 
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// 将datatable 转换成带class=gridview的table
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DT2GridView(DataTable dt)
        {
            string td = "<td>{0}<td>";

            if (dt == null || dt.Rows.Count <= 0)
            {
                return "";
            }

            string html = "<table class='gridview'>";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                html += "<tr>";

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    html += string.Format(td, dt.Rows[i][j]);
                }
                html += "</tr>";
            }

            html += "</table>";

            return html;
        }
    }
}
